Work and Energy¶
import numpy as np
%matplotlib inline
import matplotlib.pyplot as plt
from matplotlib import animation, rc
from IPython.display import HTML
Tutorial Problem 3.1¶
Use work-energy theorem on projectile motion
def v(v0, t, g=9.81):
return v0 - g*t
def x(x0, v0, t, g=9.81):
return x0 + v0*t - g*t**2/2
v0 = 200 # initial velocity
g = 9.81 #m/s2
x0 = 0
m = 100 #kg
time = np.arange(0, 40.8, 0.1)
velocity = v(v0, time)
h = x(x0, v0, time) # height
ke = 0.5 * m * velocity**2 # kinetic energy
gpe = m * g * h # gravitational potential energy
print("Speed of rock when it leaves the ground = %.f m/s" % v0)
print("Speed of rock when it reaches the ground = %.3f m/s" % (velocity[-1]))
if np.allclose(abs(velocity[0]), abs(velocity[-1]), 1) == True: # error within 1m/s
print("Speed at which the rock leaves the ground is equal to that when it reaches the ground")
else:
print("Error")
Speed of rock when it leaves the ground = 200 m/s
Speed of rock when it reaches the ground = -199.267 m/s
Speed at which the rock leaves the ground is equal to that when it reaches the ground
nframes = len(time)
# Plot background axes
fig, axes = plt.subplots(1,3, figsize=(18,10))
# define lines
line1, = axes[0].plot([], [], 'ko', lw=2)
line2, = axes[1].plot([], [], 'ro', lw=2)
line3, = axes[2].plot([], [], 'bo', lw=2)
# customise axes
axes[0].set_xlim((-0.5, 0.5))
axes[0].set_ylim((0, 2040))
axes[0].set_ylabel('height (m)')
axes[0].set_title('Trajectory of rock')
axes[1].set_xlim((-0.5, 0.5))
axes[1].set_ylim((0, 2000000))
axes[1].set_title('KE of rock over time fast forward x10')
axes[2].set_xlim((-0.5, 0.5))
axes[2].set_ylim((0, 2000000))
axes[2].set_title('GPE of rock over time fast forward x10')
lines = [line1, line2, line3]
plt.subplots_adjust(hspace=0.5)
# Plot background for each frame
def init():
for line in lines:
line.set_data([], [])
return lines
# Set what data to plot in each frame
def animate(i):
x1 = 0
y1 = h[i]
lines[0].set_data(x1, y1)
x2 = 0
y2 = ke[i]
lines[1].set_data(x2, y2)
x3 = 0
y3 = gpe[i]
lines[2].set_data(x3, y3)
return lines
# Call the animator
anim = animation.FuncAnimation(fig, animate, init_func=init,
frames=nframes, interval=10, blit=True)
HTML(anim.to_jshtml())